var config = $.extend(true, {}, listApp, {
    data: {
        service: settingsService,
        iconUrl: '',
        searchVm: {},
        noSetting: false,
        inited: true,
        settingNames: [
            'deviceId',
            'deviceName',
            'serverId',
            'logo',
            'remark'
        ],
        settings: {
            deviceId: ''
        },
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        editVm: {
            editFormRules: {
                formRules: {
                    deviceName: [{required: true, message: '请输入智件名称', trigger: 'change'}],
                    channelCode: [{required: true, message: '请输入通道号', trigger: 'change'}],
                    dataType: [{required: true, message: '请输入类型', trigger: 'change'}]
                }
            }
        },
        // requestPagePath:'requestPageList',
        deviceList: []
    },
    methods: {
        init: function () {
            this.getRegId()
            this.getSettings()
        },
        getRegId() {
            const _this = this;
            _HTTP.get("/api/settings/getRegId", null, {
                success(res) {
                    _this.settings.deviceId = res.content;
                },
                fail(res) {
                    console.log(res)
                }
            })
        },
        getSettings() {
            const _this = this;
            this.service.getSettings(this.settingNames, {
                success(res) {
                    // console.log(res)
                    if (res == null || res.length == 0) {
                        // _this.$message({
                        //     msg: "没有智件ID，请联系管理员，进行正确的初始化设置！",
                        //     type: 'warning'
                        // })
                        // _this.inited = false;
                        // return;
                        _this.noSetting = true;
                    } else {
                        _this.noSetting = false;
                        res.forEach(function (map) {
                            // if (map.key === 'deviceId') {
                            //     _this.settings.deviceId = map.value
                            // }
                            if (map.key === 'deviceName') {
                                _this.settings.deviceName = map.value
                            }
                            if (map.key === 'serverId') {
                                _this.settings.serverId = map.value
                            }
                            if (map.key === 'logo') {
                                _this.settings.logo = map.value
                            }
                            if (map.key === 'remark') {
                                _this.settings.remark = map.value
                            }
                        })
                        // console.log(_this.settings)
                        _this.editVm.data = _this.settings
                        // console.log(_this.getCTX())
                        if (null != _this.settings.logo && _this.settings.logo !== '') {
                            _this.iconUrl = top._CTX + _this.settings.logo;
                        } else {
                            _this.iconUrl = null
                        }
                        // console.log(_this.iconUrl)
                    }
                },
                fail(res) {
                    console.log(res)
                }
            })
        },
        handleAvatarSuccess(res, file) {
            this.editVm.data.logo = res.data.path
            this.iconUrl = top._CTX + res.data.path
        },
        beforeAvatarUpload(file) {
            const isJpg = file.type === 'image/jpeg';
            const isPng = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJpg) {
                if (!isPng) {
                    this.$message.error('只能上传图片!');
                }
            }
            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 2MB!');
            }
            return (isPng || isJpg) && isLt2M;
        },
        mSave: function (vm) {
            if (!this.inited) {
                this.$message({
                    msg: '没有智件ID，请联系管理员，进行正确的初始化设置！',
                    type: 'warning',
                })
                return;
            }
            const _this = this
            this.$refs[vm.formName].validate(function (valid) {
                if (valid) {
                    if (_this.noSetting) {
                        _this.service.saveSettings(_this.editVm.data, {
                            success(res) {
                                // console.log(res)
                                _this.saveSuccess();
                            },
                            fail(res) {
                                // console.log(res)
                                _this.saveFail();
                            }
                        })
                    } else {
                        console.log(_this.editVm.data)
                        _this.service.updateSettings(_this.editVm.data, {
                            success(res) {
                                // console.log(res);
                                _this.saveSuccess();
                            },
                            fail(res) {
                                // console.log(res);
                                _this.saveFail();
                            }
                        })
                    }
                } else {
                    _this.showFailMsg('校验不通过哦');
                    return false;
                }
            });
        },
        handleRemove(file) {
            console.log(file);
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        saveSuccess() {
            const _this = this;
            this.$message({
                message: '保存成功！',
                type: 'success'
            });
            setTimeout(() => {
                parent.window.location.reload()
            }, 1000)
        },
        saveFail() {
            this.$message({
                message: '保存失败！',
                type: 'fail'
            });
        },
        getCTX: function () {
            const href = parent.window.location.href
            const pathname = parent.window.location.pathname
            const pathIndex = href.indexOf(pathname)
            const topUrl = href.substring(0, pathIndex)
            return topUrl
        }
    }
});
var app = new Vue(config);
